﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace 字符串转换整数__atoi_
{
    class Program
    {
        static void Main(string[] args)
        {

            string test = "   -42";
            Solution solution = new Solution();
            Console.WriteLine(solution.MyAtoi(test));
            Console.ReadLine();
        }
    }

    public class Solution
    {
        public int MyAtoi(string s)
        {
            if (s == "")
            {
                return 0;
            }
            int num = 0;
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] != ' ')
                {
                    break;
                }
                else
                {
                    num++;
                }
            }
            s = s.Remove(0, num);
            string b ="";
            for (int i = 0; i < s.Length; i++)
            {
                if (isNum(s[i]) && s[i] != '0')
                {
                    b = s.Substring(0, i);
                    //s = s.Substring(i, s.Length - i);
                    break;
                }
            }
            //if (b.Replace("0", "").Length > 1)
            //{
            //    return 0;
            //}
            int num1 = 0;
            for (int i = 0; i < b.Length; i++)
            {
                if (!isNum(b[i])&&b[i]!=' ' && b[i] != '-'&& b[i] != '+')
                {
                    return 0;
                }
                if (b[i] == '-' || b[i] == '+')
                {
                    num1++;
                    if (num1 > 1)
                    {
                        return 0;
                    }
                }
            }
            bool aa = true;
            if (b.Length > 0)
            {
                if (b[b.Length - 1] == '-')
                {
                    aa = false;
                }
                else if (b[b.Length - 1] == '+')
                {
                    aa = true;
                }
            }
            int result = 0;
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < s.Length; i++)
            {
                if (isNum(s[i]))
                {
                    stringBuilder.Append(s[i]);
                }
                else if (s[i]=='-'||s[i]=='+')
                {

                }
                else
                {
                    break;
                }
            }
            if (stringBuilder.ToString() == "")
            {
                return 0;
            }
            if (stringBuilder.Length > 10)
            {
                return aa ? int.MaxValue : int.MinValue;
            }
            else
            {
                long a = long.Parse(stringBuilder.ToString());
                if (!aa)
                {
                    a = -a;
                }
                if (a >= int.MinValue && a <= int.MaxValue)
                {
                    result = (int)a;
                }
                else
                {
                    return a< int.MinValue? int.MaxValue : int.MinValue;
                }
                return result;
            }
        }
        private bool isNum(char c)
        {
            if (c >= '0' && c <= '9')
            {
                return true;
            }
            return false;
        }
    }
}
